package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.PortBlueRuleDto;
import com.hualude.roadsign.dto.PortRedRuleDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/12/9.
 */
public class PortBlueRuleRepositoryImpl {
    @PersistenceContext
    private EntityManager em;

    public List<PortBlueRuleDto> getPortBlueRule4DT(DTRequestDto dtRequestDto, Integer alarmId) {
        String sql = "select * from tb_port_blue_rule where alarmId=:alarmId";
        Query query = em.createNativeQuery(sql);
        query.setParameter("alarmId", alarmId);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(PortBlueRuleDto.class));
        return query.setFirstResult(dtRequestDto.getStart()).setMaxResults(dtRequestDto.getLength()).getResultList();
    }


    public List<PortRedRuleDto> getBlueRuleByModelId(DTRequestDto dtRequestDto, Integer modelId)throws Exception{
       String sql="select BlueRuleId,RuleModelId,BlueRuleLevel,BlueRuleName,BlueRuleContent,BlueRuleLimitTime," +
               "BlueRuleFlag from tb_port_blue_rule where RuleModelId=:modelId order by BlueRuleFlag,BlueRuleLevel ";
        Query query = em.createNativeQuery(sql);
        query.setParameter("modelId", modelId);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(PortBlueRuleDto.class));
        return query.setFirstResult(dtRequestDto.getStart()).setMaxResults(dtRequestDto.getLength()).getResultList();
    }

    public Integer getBlueRuleCountByModelId(DTRequestDto dtRequestDto, Integer modelId)throws Exception{
        String sql="select count(*) from tb_port_blue_rule where RuleModelId=:modelId";
        Query query = em.createNativeQuery(sql);
        query.setParameter("modelId", modelId);
        return Integer.parseInt(query.getSingleResult().toString());
    }
}
